package protocol

import (
	"bytes"
	"encoding/binary"
	"io"
)

// Decode 解码
func Decode(reader io.Reader) (string, error) {
	lengthByte := make([]byte, 4)
	_, err := reader.Read(lengthByte)
	if err != nil {
		return "", err
	}
	return "", nil
}

// Encode 编码
func Encode(reqMethod byte, message string) ([]byte, error) {
	length := int32(len(message))
	var pkg = new(bytes.Buffer)
	err := binary.Write(pkg, binary.BigEndian, length)
	if err != nil {
		return nil, err
	}

	err = binary.Write(pkg, binary.BigEndian, reqMethod)
	if err != nil {
		return nil, err
	}

	err = binary.Write(pkg, binary.BigEndian, message)
	if err != nil {
		return nil, err
	}
	return pkg.Bytes(), nil
}
